<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use think\facade\View;
use think\exception\ValidateException;
use app\admin\BaseController;
use app\admin\model\Stop as StopModel;
use app\admin\validate\Stop as Validate;
/**
 * Stop管理
 */
class Stop extends BaseController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("index")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            $input = input("post.");
            $count = StopModel::withSearch(["keyword"], $input)->count();
            $data  = StopModel::withSearch(["keyword"], $input)->order($input["prop"], $input["order"])->page($input["page"], $input["pageSize"])->select();
            foreach ($data as &$datum){
                if($datum['image_arr']){
                    $datum['image_arr']=json_decode($datum['image_arr']);
                }
            }
            return json(["status" => "success", "message" => "请求成功", "data" => $data, "count" => $count]);
        } else {
            $merchant=(new \app\admin\model\Merchant())->where('status',1)->select()->toArray();
            View::assign('merchant', $merchant);
            return View::fetch();
        }
    }
    
    /**
     * 保存新建的资源
     */
    public function save()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("save")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            $input['image_arr']=$input['image_arr']?json_encode($input['image_arr']):"";
            StopModel::create($input);
            return json(["status" => "success", "message" => "添加成功"]);
        }
    }
    
    /**
     * 保存更新的资源
     */
    public function update()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("update")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            $input['image_arr']=$input['image_arr']?json_encode($input['image_arr']):"";
            StopModel::update($input);
            return json(["status" => "success", "message" => "修改成功"]);
        }
    }
    
    /**
     * 删除指定资源
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                validate(Validate::class)->scene("delete")->check($input);
            } catch ( ValidateException $e ) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            StopModel::destroy($input["ids"]);
            return json(["status" => "success", "message" => "删除成功"]);
        }
    }
}